/*
 * Copyright (c) 2006-2025, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2025-09-29     Guotong    first version
 */

#include "key.h"
#include <rtdbg.h>

#define DBG_TAG "key"
#define DBG_LVL DBG_LOG

/* 全局变量 */
static struct rt_timer key_timer;  // 静态定时器对象
static rt_uint32_t key_press_time = 0;
static rt_bool_t key_state = RT_FALSE;
static key_callback_t key_event_callback = RT_NULL;
static rt_uint8_t debounce_count = 0;  // 消抖计数器
static rt_uint32_t last_state = 1;  // 上次按键状态

/**
 * @brief  按键检测定时器回调函数
 * @param  parameter: 未使用
 * @retval 无
 */
static void key_timer_callback(void *parameter)
{
    rt_uint32_t current_state = HAL_GPIO_ReadPin(KEY_PORT, KEY_PIN);
    
    /* 非阻塞式按键消抖 - 状态机方式 */
    if (current_state != last_state)
    {
        // 状态变化，重置消抖计数
        debounce_count = 0;
    }
    else if (debounce_count < (KEY_DEBOUNCE_TIME / 10))  // 10ms定时器，计算需要的次数
    {
        // 状态未变化，增加消抖计数
        debounce_count++;
    }
    else
    {
        // 消抖完成，确认状态稳定
        if (current_state == 0 && key_state == RT_FALSE)  // 按键按下
        {
            key_state = RT_TRUE;
            key_press_time = rt_tick_get();
            LOG_D("Key pressed");
        }
        else if (current_state == 1 && key_state == RT_TRUE)  // 按键释放
        {
            key_state = RT_FALSE;
            rt_uint32_t press_duration = rt_tick_get() - key_press_time;
            
            if (press_duration >= KEY_LONG_PRESS_TIME)  // 长按
            {
                LOG_D("Long press detected");
                if (key_event_callback != RT_NULL)
                {
                    key_event_callback(KEY_EVENT_LONG_PRESS);
                }
            }
            else if (press_duration >= KEY_SHORT_PRESS_TIME)  // 短按
            {
                LOG_D("Short press detected");
                if (key_event_callback != RT_NULL)
                {
                    key_event_callback(KEY_EVENT_SHORT_PRESS);
                }
            }
        }
    }
    
    // 更新上次状态
    last_state = current_state;
}

/**
 * @brief  初始化按键
 * @param  无
 * @retval 无
 */
void key_init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct = {0};

    /* 启用GPIO时钟 */
    __HAL_RCC_GPIOA_CLK_ENABLE();

    /* 配置按键引脚 */
    GPIO_InitStruct.Pin = KEY_PIN;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    HAL_GPIO_Init(KEY_PORT, &GPIO_InitStruct);
    
    LOG_D("Key initialized on PA0");
}

/**
 * @brief  初始化按键检测定时器
 * @param  无
 * @retval 无
 */
void key_timer_init(void)
{
    /* 初始化静态定时器 */
    rt_timer_init(&key_timer,
                 "key_timer",
                 key_timer_callback,
                 RT_NULL,
                 10,  // 10ms定时器
                 RT_TIMER_FLAG_PERIODIC);
    
    /* 启动定时器 */
    rt_timer_start(&key_timer);
    
    LOG_D("Key timer initialized (10ms period)");
}

/**
 * @brief  注册按键事件回调函数
 * @param  callback: 回调函数指针
 * @retval 无
 */
void key_register_callback(key_callback_t callback)
{
    key_event_callback = callback;
}
